package com.lanlian.cayman.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.ArrayList;

/**
 * 投资风险调研选择控件
 */
public class RiskSelectView extends LinearLayout {
    Activity context;
    ArrayList<String> list;
    ArrayList<ImageView> views;
    int select=-1;


    public RiskSelectView(Context context) {
        super(context);
        this.context=(Activity) context;
    }
    public RiskSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=(Activity) context;
    }
    private void initView()
    {
        removeAllViews();
        LinearLayout ll=new LinearLayout(context);
        LinearLayout ll_item=null;
        LayoutParams lp_text=new LayoutParams(0,LayoutParams.MATCH_PARENT);
        lp_text.gravity=Gravity.LEFT;
        LayoutParams lp=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
        lp.gravity= Gravity.LEFT;
        lp.leftMargin= DensityUtil.dp2px(20);
        lp.rightMargin=DensityUtil.dp2px(16);
        lp.topMargin=DensityUtil.dp2px(16);
        lp_text.weight=1;
        for(int i=0;i<list.size();i++)
        {
            if(i%3==0)
            {
                ll_item=new LinearLayout(context);
                ll_item.setLayoutParams(lp);

                addView(ll_item);

            }

            LayoutInflater inflater = context.getLayoutInflater();
            View view = inflater.inflate(R.layout.view_risk_textitem, ll, false);

            view.setLayoutParams(lp_text);
            TextView tv=view.findViewById(R.id.tv);
            ImageView iv=view.findViewById(R.id.iv);
            tv.setText(list.get(i));
            ll_item.addView(view);

            if(i==list.size()-1)
            {
                if(i%3==1)
                {
                    View view1 = inflater.inflate(R.layout.view_risk_textitem, ll, false);
                    view1.setLayoutParams(lp_text);
                    view1.setVisibility(INVISIBLE);
                    ll_item.addView(view1);
                }
                else  if(i%3==2)
                {
                    View view1 = inflater.inflate(R.layout.view_risk_textitem, ll, false);
                    view1.setLayoutParams(lp_text);
                    view1.setVisibility(INVISIBLE);

                    View view2 = inflater.inflate(R.layout.view_risk_textitem, ll, false);
                    view2.setLayoutParams(lp_text);
                    view2.setVisibility(INVISIBLE);
                    ll_item.addView(view1);
                    ll_item.addView(view2);
                }
            }
        }
    }
    public  void setData(ArrayList<String> list)
    {
        this.list=list;
        if(list==null)
        {
            return;
        }
        initView();
    }

}
